#ifndef ELICON_H
#define ELICON_H

#include <QImage>
#include <QWidget>

class QSvgWidget;
class QPropertyAnimation;

class ElIcon : public QWidget
{
    Q_OBJECT
public:
    explicit ElIcon(const QString &fileName, QWidget *parent = nullptr);

    ElIcon &setFileName(const QString &fileName);
    bool isNull() const;

    QColor background() const;
    // it only works when icon is svg, otherwise it will return false.
    bool fillBackground(const QColor &color);

    qreal angle() const;
    void setAngle(qreal angle);

public slots:
    // only effective for non-svg icons, because svg supports animations natively.
    void setRotatable(bool b);

protected:
    void paintEvent(QPaintEvent *e) override;
    void showEvent(QShowEvent *e) override;
    void resizeEvent(QResizeEvent *e) override;

signals:
    void iconChanged(const QString &fileName);

private:
    Q_PROPERTY(QColor background READ background WRITE fillBackground)
    Q_PROPERTY(qreal angle READ angle WRITE setAngle)

    QString _fileName;
    QImage _img;
    QSvgWidget *_svgWidget = nullptr;
    QColor _bgColor = Qt::transparent;
    QPropertyAnimation *_animRotate = nullptr;
    qreal _angle{0}; // used for ratation
};

#endif // ELICON_H
